<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="商品分类" prop="oneCategoryId">
        <el-select
          v-model.number="queryParams.oneCategoryId"
          @change="getTwoCategory"
          placeholder="请选择分类"
        >
          <el-option
            v-for="item in deptOptions"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="twoCategoryId">
        <el-select
          v-model.number="queryParams.twoCategoryId"
          @change="getThreeCategory"
          placeholder="请选择二级分类"
        >
          <el-option
            v-for="item in twoCategory"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="threeCategoryId">
        <el-select
          v-model.number="queryParams.threeCategoryId"
          placeholder="请选择三级分类"
          @change="getThreeId"
        >
          <el-option
            v-for="item in threeCategory"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入商品名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="品牌">
        <el-select
          v-model="queryParams.goodsBrandId"
          filterable
          placeholder="请选择品牌"
        >
          <el-option
            v-for="item in brandList"
            :key="item.id"
            :label="item.brandName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="30">
      <el-col :span="12">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>待选择商品</span>
            <div style="float: right">
              <el-button type="primary" @click="checkProduct">选择</el-button>
            </div>
          </div>
          <el-table
            ref="proTable"
            v-loading="loading"
            :data="productList"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="品牌" align="center" prop="goodsBrand">
              <template slot-scope="scope">
                <el-image
                  v-if="scope.row.goods_brand_id"
                  style="width: 50px"
                  :src="
                    'https://img-cdn.duobashuzi.com/storage/' +
                    scope.row.goods_brand_id.logo
                  "
                  fit="fill"
                ></el-image>
                <span v-else>无</span>
              </template>
            </el-table-column>
            <el-table-column label="商品名称" align="center" prop="name" />
            <el-table-column label="图片" align="center" prop="productName">
              <template slot-scope="scope">
                <el-image
                  style="width: 100px; height: 100px"
                  :src="scope.row.preview_img"
                  fit="fit"
                  :preview-src-list="[scope.row.preview_img]"
                ></el-image>
              </template>
            </el-table-column>
            <el-table-column
              label="市场价格"
              align="center"
              prop="market_price"
            />
            <el-table-column
              label="成本价格"
              align="center"
              prop="cost_price"
            />
            <el-table-column label="售价" align="center" prop="shop_price" />
          </el-table>
          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
          <!-- <div style="text-align: right; margin-top: 15px">

          </div> -->
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>已选择商品</span>
            <div style="float: right">
              <el-button type="primary" :disabled="multiple" @click="subProduct"
                >提交</el-button
              >
              <el-button type="danger" :disabled="multiple" @click="delProduct"
                >移除</el-button
              >
            </div>
          </div>
          <el-table
            v-loading="loading"
            :data="selectedProductList"
            @selection-change="handleDelOrSub"
          >
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="品牌" align="center" prop="goodsBrand">
              <template slot-scope="scope">
                <el-image
                  v-if="scope.row.goods_brand_id"
                  style="width: 50px"
                  :src="
                    'https://img-cdn.duobashuzi.com/storage/' +
                    scope.row.goods_brand_id.logo
                  "
                  fit="fill"
                ></el-image>
                <span v-else>无</span>
                <!-- <el-image
                  style="width: 50px"
                  :src="scope.row.goods_brand_id.logo"
                  fit="fill"
                ></el-image> -->
              </template>
            </el-table-column>
            <el-table-column label="商品名称" align="center" prop="name" />
            <el-table-column label="图片" align="center" prop="preview_img">
              <template slot-scope="scope">
                <el-image
                  style="width: 100px; height: 100px"
                  :src="scope.row.preview_img"
                  fit="fit"
                  :preview-src-list="[scope.row.preview_img]"
                ></el-image>
              </template>
            </el-table-column>
            <el-table-column
              label="市场价格"
              align="center"
              prop="market_price"
            />
            <el-table-column
              label="成本价格"
              align="center"
              prop="cost_price"
            />
            <el-table-column label="售价" align="center" prop="shop_price" />
          </el-table>
        </el-card>
      </el-col>
    </el-row>
    <el-dialog
      title="添加商品信息"
      :visible.sync="open"
      width="400px"
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="价格模式" prop="priceModel">
          <el-radio-group v-model="form.priceModel">
            <el-radio :label="1">议价</el-radio>
            <el-radio :label="2">自定义</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="议价方式"
          prop="bargainId"
          v-if="form.priceModel == 1"
        >
          <el-select v-model="form.bargainId" placeholder="请选择议价方式">
            <el-option
              v-for="item in bargainList"
              :key="item.id"
              :label="item.bargainName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否扣点" prop="isPointBuckle">
          <el-radio-group v-model="form.isPointBuckle">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="品牌扣点"
          prop="pointBuckleId"
          v-if="form.isPointBuckle == 1"
        >
          <el-select v-model="form.pointBuckleId" placeholder="请选择品牌扣点">
            <el-option
              v-for="item in pointList"
              :key="item.id"
              :label="item.pointBuckleName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否返现" prop="isCashBank">
          <el-radio-group v-model="form.isCashBank">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="返现方式"
          prop="cashBankId"
          v-if="form.isCashBank == 1"
        >
          <el-select v-model="form.cashBankId" placeholder="请选择返现方式">
            <el-option
              v-for="item in cashbackList"
              :key="item.id"
              :label="item.cashBankName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="自定义价格"
          prop="setProductShopPrice"
          v-if="form.priceModel == 2"
        >
          <el-input
            v-model="form.setProductShopPrice"
            placeholder="请输入自定义价格"
            onkeyup="value=value.replace(/[^\d]/g,'')"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="支付类别" prop="payTypeIds">
          <el-select
            v-model="form.payTypeIds"
            multiple
            placeholder="请选择支付类别"
          >
            <el-option
              v-for="item in payTypeList"
              :key="item.id"
              :label="item.payTypeName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="库存数" prop="localStockNum">
          <el-input-number
            v-model="form.localStockNum"
            :min="1"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="subLoad" type="primary" @click="submitFileForm"
          >确 定</el-button
        >
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  category,
  categoryGoods,
  bargainList,
  showHomePayType,
  saveMallProduct,
  showBrandList,
  showPointBuckleList,
  showBankPriceList,
} from "@/api/system/product";

export default {
  data() {
    return {
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        oneCategoryId: null,
        twoCategoryId: null,
        threeCategoryId: null,
      },
      subLoad: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      total: 0,
      deptOptions: null,
      twoCategory: [],
      threeCategory: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
      //待选择的商品列表
      productList: [],
      //选择后的商品列表
      selectedProductList: [],
      //选择状态下的商品列表
      middleProductList: [],
      //待删除或待提交的商品
      delProductList: [],
      //议价列表
      bargainList: [],
      //支付方式列表
      payTypeList: [],
      //返现列表
      showBankPriceList: [],
      form: {
        localStockNum: 1,
      },
      rules: {
        bargainId: [
          { required: true, message: "请选择议价方式", trigger: "change" },
        ],
        setProductShopPrice: [
          { required: true, message: "请输入自定义价格", trigger: "blur" },
        ],
        // payTypeIds: [
        //   { required: true, message: "请选择支付类别", trigger: "click" },
        // ],
        isPointBuckle: [
          { required: true, message: "请选择是否扣点", trigger: "blur" },
        ],
        pointBuckleId: [
          { required: true, message: "请选择品牌扣点", trigger: "change" },
        ],
      },
      brandList: [],
      pointList: [],
    };
  },
  created() {
    this.getDeptTree();
    this.getBrandList();
    this.getPointBrand();
    this.getCashbackList();
  },
  methods: {
    getCashbackList() {
      showBankPriceList().then((res) => {
        this.cashbackList = res.data;
      });
    },
    getBrandList() {
      showBrandList().then((res) => {
        this.brandList = res.data;
      });
    },
    checkProduct() {
      const newArr = this.middleProductList.filter((v) =>
        this.selectedProductList.every((val) => val.id != v.id)
      );
      this.selectedProductList = [...this.selectedProductList, ...newArr];
    },
    subProduct() {
      this.open = true;
      this.reset();
      this.getBargainList();
    },
    // 表单重置
    reset() {
      this.form = {
        payTypeIds: null,
        bargainId: null,
        localStockNum: 1,
        priceModel: 1,
        isPointBuckle: 1,
      };
      this.resetForm("form");
    },
    //获取扣点品牌
    getPointBrand() {
      showPointBuckleList().then((res) => {
        this.pointList = res.data;
      });
    },
    submitFileForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.delProductList.length > 0) {
            const ids = this.delProductList.map((item) => item.id);
            const data = {
              ...this.form,
              mallId: this.$route.query.id,
              productIds: ids,
            };
            this.subLoad = true;
            saveMallProduct(data)
              .then((res) => {
                this.subLoad = false;
                this.open = false;
                this.$message({
                  message: res.msg,
                  type: "success",
                });
              })
              .catch((err) => {
                this.subLoad = false;
              });
          } else {
          }
        }
      });
    },
    //获取议价列表
    getBargainList() {
      const mallId = this.$route.query.id;
      bargainList({ mallId }).then((res) => {
        this.bargainList = res.data;
      });
    },

    //移除
    delProduct() {
      this.selectedProductList = this.selectedProductList.filter((v) =>
        this.delProductList.every((val) => v.id != val.id)
      );
      this.delProductList = [];
    },
    //多选
    handleDelOrSub(selection) {
      this.delProductList = selection;
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(selection);
      this.middleProductList = selection;
    },

    //获取二级分类
    getTwoCategory(e) {
      const twoCategory = this.deptOptions.find((v, i) => {
        return v.id == e;
      });
      this.twoCategory = twoCategory.children;
      this.threeCategory = [];
      this.queryParams.twoCategoryId = null;
      this.queryParams.threeCategoryId = null;
    },
    //获取三级分类
    getThreeCategory(e) {
      const threeCategory = this.twoCategory.find((v, i) => {
        return v.id == e;
      });
      this.threeCategory = threeCategory.children;
      this.queryParams.threeCategoryId = null;
    },
    getThreeId(e) {
      console.log(typeof e);
      console.log(typeof this.queryParams.threeCategoryId);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      categoryGoods(this.queryParams).then((res) => {
        console.log(res);
        this.productList = res.data.data;
        this.total = res.data.total;
        this.loading = false;
      });
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      category().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    //获取选中的分类
    getCheckedNodes() {
      console.log(this.$refs.tree.getCheckedNodes());
    },
  },
};
</script>
<style lang="scss" scoped>
.c_left {
  height: 88vh;
  background-color: #f9f9f9;
}
::v-deep .el-tree {
  background-color: transparent;
}
</style>
